﻿using FCModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FCBLL
{
    public class cmmBll : IFormatBLL
    {
        int iHeadIndex = 0;
        int iValueIndex = 1;

        public IList<ParameterDao> AnalysisFile(string[] arrSourceFile, string fileType)
        {
            IList<ParameterDao> listSSD = new List<ParameterDao>();
            string HeadFlag = arrSourceFile[0].Substring(0, 20);
            ParameterDao ssd = new ParameterDao();
            //标志是否有连续多行多个参数计数
            int iContinueRow = 1;
            int iSeqNo = 0;
            bas_TestType btt = CommonBLL.getValueIndexAndHeadIndex(fileType);
            //所有需要采集的参数  (p.isDelete.Equals(false) || p.isDelete == null)
            List<CMMParameter> paramList = new BaseRepositoryBLL<CMMParameter>().
                LoadEntities(p => p.isEnable.Equals(true)).ToList<CMMParameter>();

            iHeadIndex = string.IsNullOrWhiteSpace(btt.HeadIndex.ToString()) ? iHeadIndex : (Int32)btt.HeadIndex;
            iValueIndex = string.IsNullOrWhiteSpace(btt.ValueIndex.ToString()) ? iValueIndex : (Int32)btt.ValueIndex;

            for (int i = iValueIndex; i < arrSourceFile.Length; i++)
            {
                string[] tmpArrName = arrSourceFile[i].Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                //
                if (i == arrSourceFile.Length - 1)
                {
                    listSSD.Add(ssd);
                }

                if (arrSourceFile[i].Contains(HeadFlag))
                {
                    if (ssd.SeqNo == iContinueRow.ToString())
                    {
                        iContinueRow++;
                        listSSD.Add(ssd);
                    }

                    iSeqNo++;
                    ssd = new ParameterDao();
                    ssd.SeqNo = iSeqNo.ToString();

                    //iContinueRow = 0;
                    continue;
                }


                if (arrSourceFile[i].Trim().Length == 0) continue;

                // if (tmpArrName[1] == "Vectorial")
                bool isNeed = false;
                int paramSeq = 0;
                int valueSeq = 6;
                foreach (CMMParameter Parameter in paramList)
                {
                    if (tmpArrName.Contains(Parameter.ParameterName.Trim()))
                    {
                        isNeed = true;
                        paramSeq = Parameter.paramSeq;
                        if (Parameter.valueSeq != null && Parameter.valueSeq != 0)
                        {
                            valueSeq = Convert.ToInt32(Parameter.valueSeq);
                        }
                        break;
                    }
                }
                if (isNeed)
                {
                    try
                    {
                        string[] tmpArrValue = arrSourceFile[i + 1].Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                        Parameter ss = new Parameter();

                        ss.ParameterName = tmpArrName[paramSeq].Trim();
                        //ss.ParameterSortName = "";

                        ss.ParameterValue = (float)Math.Abs(Math.Round(float.Parse(tmpArrValue[valueSeq].Trim('\0')), 5, MidpointRounding.AwayFromZero));// float.Parse(tmpArrValue[6]);
                        ssd.listSSD.Add(ss);
                        i++;
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("请检测CMM参数配置_" + tmpArrName[paramSeq].Trim());
                    }
                }
            }
            return listSSD;
        }

        #region
        /*
                string[] tmpArrData = rowData.Split(new char[] { ' ', '\n' }, StringSplitOptions.RemoveEmptyEntries);
                if (rowData.Contains(HeadFlag))
                {
                    ssd = new ParameterDao();
                    ssd.SeqNo = iSeqNo.ToString();
                    iContinueRow = 0;
                    continue;
                }
                if (tmpArrData.Length == 0) continue;
                if (tmpArrData[0].Equals("EOT", StringComparison.OrdinalIgnoreCase))
                {
                    if (ssd.listSSD.Count > 0)
                        listSSD.Add(ssd);
                    iContinueRow = 0;
                    iSeqNo++;
                }
                if (tmpArrData[0].Equals("EOM", StringComparison.OrdinalIgnoreCase))
                {
                    iContinueRow = 0;
                }
                else
                    iContinueRow++;
                if (iContinueRow == 0 || tmpArrData.Length < 2)
                    continue;//过滤EOM/EOT行
                string[] paramName = tmpArrData[tmpArrData.Length - 1].Split(new char[] { '&' }, StringSplitOptions.RemoveEmptyEntries);
                Parameter ss = new Parameter();
                if (paramName.Length > 1)//多个参数在同一行
                {
                    ss.ParameterName = paramName[iContinueRow - 1];
                }
                else
                {
                    ss.ParameterName = tmpArrData[tmpArrData.Length - 1];
                }
                try
                {
                    string[] tmparrsortname = ss.ParameterName.Split('-');
                    ss.ParameterSortName = tmparrsortname[0].Length == 4 ? tmparrsortname[0].Substring(0, 3) + "0" + tmparrsortname[0].Substring(3, 1) : tmparrsortname[0];
                    if (tmparrsortname.Length > 1)
                        ss.ParameterSortName += "-" + (tmparrsortname[1].Length == 2 ? tmparrsortname[1] : "0" + tmparrsortname[1]);
                }
                catch { }
                ss.ParameterValue = Math.Abs(float.Parse(tmpArrData[1])); //Abs目前是负号只是坐标的方向，并不代表值的大小
                //通过基础数据获取标准值
                //ss.StandardValue = float.Parse(tmpArrData[2]);
                ssd.listSSD.Add(ss);
            */
        #endregion
    }
}
